package com.resou.reader.signin.style2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.activity.limitedrecharge.LimitedRechargeActivity;
import com.resou.reader.data.signin.model.SignInInfo2;
import com.resou.reader.reader.utils.ScreenUtils;
import com.resou.reader.utils.TimeUtil;
import com.resou.reader.utils.ViewHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TaskAdapter extends RecyclerView.Adapter {


    private int activityCode;
    private final List<SignInInfo2.UserActivityBean.ActivityDetailBean> mActivityDetail;
    private float mDp50;

    public TaskAdapter(List<SignInInfo2.UserActivityBean.ActivityDetailBean> activityDetail, int activityCode) {
        mActivityDetail = activityDetail;
        this.activityCode = activityCode;
    }

    private static final String TAG = "TaskAdapter-App";
    private OnTaskInteractionListener mInteractionListener;
    private Activity mActivity;

    public void setInteractionListener(OnTaskInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
        mActivity = (Activity) interactionListener;
        mDp50 = ScreenUtils.convertDpToPixel(50, mActivity);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (activityCode == 2) {
            return new SignInHolder(ViewHelper.createView(viewGroup, R.layout.recycler_item_sign_in_task_item));
        }
        if (activityCode == 1) {
            return new TimeLimitedHolder(ViewHelper.createView(viewGroup, R.layout.recycler_item_time_limited_item));
        }
        if (activityCode == 3) {
            return new SignInHolder(ViewHelper.createView(viewGroup, R.layout.recycler_item_sign_in_task_item));
        }
        return new DefaultHolder(new View(viewGroup.getContext()));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SignInInfo2.UserActivityBean.ActivityDetailBean detailBean = mActivityDetail.get(position);
        int statusCode = detailBean.getStatusCode();
        if (activityCode == 2) {
            //   0:未完成;1:领取;2:已完成;3:去参加;4:进行中;5:已结束.
            if (holder instanceof SignInHolder) {
                SignInHolder signInHolder = (SignInHolder) holder;
                signInHolder.tvRuleInfo.setText(detailBean.getTaskDesc());
                signInHolder.tvTaskStatus.setText(detailBean.getTaskStatus());
                switch (statusCode) {
                    case 0://未完成
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        break;
                    case 1://领取
                        signInHolder.tvTaskStatus.setTextColor(Color.WHITE);
                        signInHolder.tvTaskStatus.getLayoutParams().width = (int) mDp50;
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_solid_button);
                        signInHolder.tvTaskStatus.setOnClickListener(v -> mInteractionListener.onReceiveClicked(detailBean.getTaskId()));
                        break;
                    case 2://已完成
                        signInHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                    case 3://去参加
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        signInHolder.tvTaskStatus.setOnClickListener(v -> {
                            mInteractionListener.onJoinClicked(detailBean.getTaskCode(), detailBean.getStatusCode());
                        });
                        break;
                    case 4://进行中
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        signInHolder.tvTaskStatus.setOnClickListener(v -> {
                            mInteractionListener.onJoinClicked(detailBean.getTaskCode(), detailBean.getStatusCode());
                        });
                        break;
                    case 5://已结束
                        signInHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                }
            }
        }
        if (activityCode == 1) {
            if (holder instanceof TimeLimitedHolder) {
                TimeLimitedHolder limitedHolder = (TimeLimitedHolder) holder;
                limitedHolder.tvActivityName.setText(detailBean.getTaskDesc());
                //截止时间
                int deadline = detailBean.getActivityCountDown();
                if (deadline > 0) {
                    limitedHolder.llCountdownContainer.setVisibility(View.VISIBLE);
                    long l = deadline * 1000;
                    mTimer = new CountDownTimer(l, 1000) {

                        //            String day;
                        String mHour = "";
                        String mMinute = "";
                        String mSecond = "";

                        @SuppressLint("SetTextI18n")
                        @Override
                        public void onTick(long millisUntilFinished) {
//                String day = TimeUtil.getDay(millisUntilFinished);
                            String hour = TimeUtil.getAllHour(millisUntilFinished);
                            String minute = TimeUtil.getStringMinute(millisUntilFinished);
                            String second = TimeUtil.getStringSecond(millisUntilFinished);
//                if (!day.equals(this.day)) {
//                    this.day = day;
//                    tvDay.setText(day + "天");
//                }
                            if (!mHour.equals(hour)) {
                                mHour = hour;
                                limitedHolder.tvHour.setText(hour + "时");
                            }
                            if (!mMinute.equals(minute)) {
                                mMinute = minute;
                                limitedHolder.tvMinute.setText(minute + "分");
                            }
                            if (!mSecond.equals(second)) {
                                mSecond = second;
                                limitedHolder.tvSecond.setText(second + "秒");
                            }
                        }

                        @Override
                        public void onFinish() {

                        }
                    };
                }
                if (mTimer != null) {
                    mTimer.start();
                }
                limitedHolder.tvTaskStatus.setText(detailBean.getTaskStatus());
                int isShowIdentity = detailBean.getActivityIdentity();
                if (isShowIdentity == 1) {
                    limitedHolder.ivFire.setVisibility(View.VISIBLE);
                }
                switch (statusCode) {
                    case 0://未完成
                        limitedHolder.tvTaskStatus.setTextColor(Color.RED);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        break;
                    case 1://领取
                        limitedHolder.tvTaskStatus.setTextColor(Color.WHITE);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_solid_button);
                        limitedHolder.tvTaskStatus.setOnClickListener(v -> mInteractionListener.onReceiveClicked(detailBean.getTaskId()));
                        break;
                    case 2://已完成
                        limitedHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                    case 3://去参加
                        limitedHolder.tvTaskStatus.setTextColor(Color.RED);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        limitedHolder.tvTaskStatus.setOnClickListener(v -> {
                            mInteractionListener.onJoinClicked(detailBean.getTaskCode(), detailBean.getStatusCode());
                            if (detailBean.getTaskCode().equals("limitRecharge")) {
                                LimitedRechargeActivity.start(mActivity, deadline);
                            }
                        });
                        break;
                    case 4://进行中
                        limitedHolder.tvTaskStatus.setTextColor(Color.RED);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        limitedHolder.tvTaskStatus.setOnClickListener(v -> {
                            mInteractionListener.onJoinClicked(detailBean.getTaskCode(), detailBean.getStatusCode());
                        });
                        break;
                    case 5://已结束
                        limitedHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        limitedHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                }
            }
        }
        if (activityCode == 3) {
            if (holder instanceof SignInHolder) {
                SignInHolder signInHolder = (SignInHolder) holder;
                signInHolder.tvRuleInfo.setText(detailBean.getTaskDesc());
                signInHolder.tvTaskStatus.setText(detailBean.getTaskStatus());
                switch (statusCode) {
                    case 0://未完成
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        break;
                    case 1://领取
                        signInHolder.tvTaskStatus.setTextColor(Color.WHITE);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_solid_button);
                        signInHolder.tvTaskStatus.setOnClickListener(v -> mInteractionListener.onReceiveClicked(detailBean.getTaskId()));
                        break;
                    case 2://已完成
                        signInHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                    case 3://去参加
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        signInHolder.tvTaskStatus.setOnClickListener(v -> {
                            mInteractionListener.onJoinClicked(detailBean.getTaskCode(), detailBean.getStatusCode());
                        });
                        break;
                    case 4://进行中
                        signInHolder.tvTaskStatus.setTextColor(Color.RED);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_red_hollow_button);
                        break;
                    case 5://已结束
                        signInHolder.tvTaskStatus.setTextColor(Color.LTGRAY);
                        signInHolder.tvTaskStatus.setBackgroundResource(R.drawable.bg_sign_in_light_hollow_button);
                        break;
                }
            }
        }
    }

    private CountDownTimer mTimer;

    @Override
    public int getItemCount() {
        return mActivityDetail == null ? 0 : mActivityDetail.size();
    }

    class SignInHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_rule_info)
        TextView tvRuleInfo;
        @BindView(R.id.tv_task_status)
        TextView tvTaskStatus;

        public SignInHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DailyHolder extends RecyclerView.ViewHolder {

        public DailyHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    class TimeLimitedHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_activity_name)
        TextView tvActivityName;
        @BindView(R.id.tv_task_status)
        TextView tvTaskStatus;
        @BindView(R.id.iv_fire)
        ImageView ivFire;
        @BindView(R.id.tv_hour)
        TextView tvHour;
        @BindView(R.id.tv_minute)
        TextView tvMinute;
        @BindView(R.id.tv_second)
        TextView tvSecond;
        @BindView(R.id.ll_countdown_container)
        LinearLayout llCountdownContainer;

        public TimeLimitedHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {

        public DefaultHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public interface OnTaskInteractionListener {
        void onToCompleteClicked();

        void onReceiveClicked(String taskId);

        void onJoinClicked(String taskCode, int statusCode);
    }

}
